﻿Imports Npgsql

Public Class Usuarios
    Private comando As New NpgsqlCommand()
    Private bd As New ConexaoBd()
    Private tblCliente As DataTable
    Private tRow As DataRow
    Private dset As DataSet
    Private limpar As New LimparDados()



    Private Sub btnSalvar_Click(sender As System.Object, e As System.EventArgs) Handles btnSalvar.Click

        Try
            If Val(txtCod.Text.ToString()) > 0 Then
                comando.CommandText = "UPDATE usuarios SET usuario = @usuario, senha = @senha WHERE idusuario = @idusuario;"

                comando.Parameters.Add("@usuario", txtUsuario.Text)
                comando.Parameters.Add("@senha", txtSenha.Text)
                comando.Parameters.Add("@idusuario", txtCod.Text)

                bd.atualizarDados(comando)

            Else
                comando.CommandText = "INSERT INTO usuarios VALUES (DEFAULT, @usuario, @senha);"

                comando.Parameters.Add("@usuario", txtUsuario.Text)
                comando.Parameters.Add("@senha", txtSenha.Text)

                bd.adicionarDados(comando)


                Dim sql As String = "SELECT MAX(idusuario) id FROM usuarios"

                'Realizando consulta
                dset = bd.carregarDados(sql, "idusuario")
                tblCliente = dset.Tables.Item(0)
                dset.Dispose()
                tRow = tblCliente.Rows(0)
                Dim id As Integer
                id = CInt(tRow("id").ToString())
                carregarCliente(id)

            End If

            'limpar.Limpar(Me)
            'txtCod.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub carregarCliente(ByVal id As Integer)
        Dim sql As String = "SELECT * FROM usuarios WHERE idusuario = " + id.ToString()

        'Realizando consulta
        dset = bd.carregarDados(sql, "usuario")
        tblCliente = dset.Tables.Item(0)
        dset.Dispose()

        If tblCliente.Rows.Count() = 1 Then
            tRow = tblCliente.Rows(0)

            txtCod.Text = tRow("idusuario").ToString()
            txtUsuario.Text = tRow("usuario").ToString()
            txtSenha.Text = tRow("senha").ToString()

            btnExcluir.Enabled = True
        Else
            MessageBox.Show("Usuário Não Encontrado!")
            btnExcluir.Enabled = False
            limpar.Limpar(Me)
        End If


    End Sub

    Private Sub txtCod_Leave(sender As System.Object, e As System.EventArgs) Handles txtCod.Leave
        If txtCod.Text.Length > 0 And Val(txtCod.Text) > 0 Then
            carregarCliente(Val(txtCod.Text))
        End If
    End Sub

    Private Sub txtCod_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCod.KeyDown
        If e.KeyCode = 13 Then
            txtUsuario.Focus()
        End If
    End Sub

    Private Sub btnPesquisar_Click(sender As System.Object, e As System.EventArgs) Handles btnPesquisar.Click

        Dim sql As String = "SELECT * FROM usuarios"
        dset = bd.carregarDados(sql, "usuarios")

        grdCliente.DataSource = dset.Tables("usuarios").DefaultView


        grdCliente.Columns(0).HeaderText = "Código"
        grdCliente.Columns(1).HeaderText = "Usuário"
        grdCliente.Columns(2).Visible = False 'Senha

    End Sub

    Private Sub grdCliente_RowHeaderMouseDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdCliente.RowHeaderMouseDoubleClick

        carregarCliente(grdCliente.CurrentRow.Cells(0).Value)
        TabControl1.SelectTab(0)
        txtUsuario.Focus()

    End Sub

    Private Sub btnNovo_Click(sender As System.Object, e As System.EventArgs) Handles btnNovo.Click
        limpar.Limpar(Me)
        txtUsuario.Focus()
    End Sub

    Private Sub btnExcluir_Click(sender As System.Object, e As System.EventArgs) Handles btnExcluir.Click
        comando.CommandText = "DELETE FROM usuarios WHERE idUsuario = @idUsuario"
        comando.Parameters.Add("@idUsuario", txtCod.Text)
        bd.atualizarDados(comando)
        limpar.Limpar(Me)
    End Sub
End Class